package com.xianyu.controller;

import com.xianyu.entity.Users;
import com.xianyu.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2021-06-21 18:46:52
 */
@RestController
@RequestMapping("users")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersService usersService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Users selectOne(Integer id) {
        return this.usersService.queryById(id);
    }

    @PostMapping("selectphone")
    public String selectphone(String userphone) {

        if (usersService.selectUserphone(userphone) != null) {
            return "1";
        } else {
            return "0";
        }
    }

    @GetMapping("selectname")
    public String selectname(String username) {

        if (usersService.selectUsername(username) != null) {
            return "1";
        } else {
            return "0";
        }
    }

    @PostMapping("userregister")
    public String registerphonepassname(@RequestParam("userphone") String userphone,@RequestParam("userpass")String userpass,@RequestParam("username")String username) {
        System.out.println(userphone);
        System.out.println(userpass);
        System.out.println(username);


        Users users = new Users();
        users.setUserphone(userphone);
        users.setUserpass(userpass);
        users.setUsername(username);

        if (username != null && userpass != null && username != null) {
            usersService.registerphonepassname(users);
            return "1";
        }else{
            return "0";
        }
        }
    @PostMapping("userlogin")
    public String loginByUsernameAndUserpass(@RequestParam("username") String username,@RequestParam("userpass") String userpass){

        Users users= usersService.loginByUsernameAndUserpass(username,userpass);
        if(users==null){
            return "0";
        }else{
            return "1";
        }
    }
 }
